package Actions;
/*
 * Author - SAMBA
 */
import java.awt.event.ActionEvent;

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.AbstractAction;
import javax.swing.JTable;

import FileParser.FileParser;
import View.ManuscriptView;

import TableModels.ReviewerTemplate;

public class ViewReviewerAction extends AbstractAction{
	JTable table;
	public ViewReviewerAction(JTable table) {
		this.table = table;
		this.putValue(NAME, "View Review");
		this.putValue(MNEMONIC_KEY, KeyEvent.VK_V);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if(table.getSelectedRow() != -1) {
						//convert to models row index, since the table index changes when it is sorted
			int row = table.convertRowIndexToModel(table.getSelectedRow());
										//must cast to correct TableModel, or getManuscriptLink wont work.
			
							
			String reviewerLink = (String) ((ReviewerTemplate)table.getModel()).getValueAt(row, 2);
			
			InputStream stream = FileParser.class.getResourceAsStream(reviewerLink);	//convert to stream
			try { // apache poi documentation  http://poi.apache.org/apidocs/index.html
				XWPFDocument docx = new XWPFDocument(stream);				//parse docx
				XWPFWordExtractor we = new XWPFWordExtractor(docx);			//extract parsed docx
				//System.out.println(we.getText());						
				ManuscriptView scriptWindow = new ManuscriptView();
				scriptWindow.start(we.getText());
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				stream.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
			
		}
	}
	


}
